package com.zrzhen.springbootdemo.common.util.collection;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;

public class MapUtil {


    public static Map<String, Object> pojo2Map(Object obj) {
        Class<?> type = obj.getClass();
        Field[] fields = type.getDeclaredFields();
        Map<String, Object> map = new HashMap<String, Object>();
        for (Field f : fields) {
            if (Modifier.isStatic(f.getModifiers()))
                continue;
            String name = f.getName();
            f.setAccessible(true);
            Object value = null;
            try {
                value = f.get(obj);
            } catch (Exception e) {
                value = null;
            }
            map.put(name, value);
        }
        type = null;fields = null;
        return map;
    }
}
